package com.cnap.service.impl;

import com.cnap.db.entity.ClusterInfoPO;
import com.cnap.db.entity.DeployClusterPO;
import com.cnap.db.entity.MicroServicePO;
import com.cnap.db.mapper.ClusterInfoMapper;
import com.cnap.db.mapper.DeployClusterMapper;
import com.cnap.model.schedule.UpdateUserChooseParam;
import com.cnap.model.schedule.engine.EngineClusterInfo;
import com.cnap.model.schedule.engine.alg.AlgDeployClusterInfo;
import com.cnap.service.ClusterInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 集群信息服务实现类
 *
 * @author xc
 **/
@Service
public class ClusterInfoServiceImpl implements ClusterInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClusterInfoServiceImpl.class);

    private final ClusterInfoMapper clusterInfoMapper;
    private final DeployClusterMapper deployClusterMapper;

    @Autowired
    public ClusterInfoServiceImpl(ClusterInfoMapper clusterInfoMapper, DeployClusterMapper deployClusterMapper) {
        this.clusterInfoMapper = clusterInfoMapper;
        this.deployClusterMapper = deployClusterMapper;
    }

    // 2.0start
    @Override
    public void saveDeployCluster(DeployClusterPO deployClusterPO) {
        DeployClusterPO old = deployClusterMapper.selectById(deployClusterPO.getId());
        if (Objects.nonNull(old)) {
            LOGGER.warn("id={} deploy cluster exist, just return", deployClusterPO.getId());
            return;
        }


        deployClusterMapper.insertDeployCluster(deployClusterPO);
    }

    @Override
    public List<DeployClusterPO> getDeployClusterBySvcId(String svcId) {
        return deployClusterMapper.selectBySvcId(svcId);
    }

    @Override
    public void deleteDeployClusterBySvcId(String svcId) {
        deployClusterMapper.deleteClusterInfoBySvcId(svcId);
    }

    @Override
    public void deleteDeployClusterBySvcIdAndClusterName(String svcId, String clusterName) {
        deployClusterMapper.deleteClusterInfoBySvcIdAndClusterName(svcId, clusterName);
    }

    // 2.0end

    /**
     * 插入或更新集群信息
     *
     * @param microServicePO 服务信息
     * @param clusterInfos   集群信息
     */
    @Override
    public void saveOrUpdateClusterInfo(MicroServicePO microServicePO, List<EngineClusterInfo> clusterInfos) {
        ClusterInfoPO oldInfo = clusterInfoMapper.selectClusterInfoBySvcId(microServicePO.getId());
        if (Objects.isNull(oldInfo)) {
            // 少了userChoose和recommendChoose
            // 全新插入
            ClusterInfoPO clusterInfoPO = new ClusterInfoPO();
            // 使用微服务id作为集群id
            clusterInfoPO.setId(microServicePO.getId());
            // 应用id
            clusterInfoPO.setAppId(microServicePO.getApplicationId());
            // 命名空间
            clusterInfoPO.setNamespace(microServicePO.getNamespace());
            // 集群信息
            clusterInfoPO.setClusterInfos(clusterInfos);
            // 插入
            clusterInfoMapper.insertClusterInfo(clusterInfoPO);
        } else {
            oldInfo.setClusterInfos(clusterInfos);
            clusterInfoMapper.updateClusterInfo(oldInfo);
        }
    }

    @Override
    public void saveOrUpdateClusterInfo(MicroServicePO microServicePO, ClusterInfoPO clusterInfoPO) {
        ClusterInfoPO oldInfo = clusterInfoMapper.selectClusterInfoBySvcId(microServicePO.getId());
        if (Objects.isNull(oldInfo)) {
            // 插入
            clusterInfoMapper.insertClusterInfo(clusterInfoPO);
        } else {
            clusterInfoMapper.updateClusterInfo(clusterInfoPO);
        }
    }

    @Override
    public void updateAlgDeployInfos(String svcId, List<AlgDeployClusterInfo> algDeployClusterInfos) {
        ClusterInfoPO oldInfo = clusterInfoMapper.selectClusterInfoBySvcId(svcId);
        if (Objects.nonNull(oldInfo)) {
            oldInfo.setAlgDeployClusterInfos(algDeployClusterInfos);
            clusterInfoMapper.updateClusterInfo(oldInfo);
        } else {
            LOGGER.warn("can't find cluster info by svcId={}", svcId);
        }
    }

    /**
     * 根据svcId删除集群信息
     *
     * @param svcId 服务id
     */
    @Override
    public void deleteClusterInfoBySvcId(String svcId) {
        deleteDeployClusterBySvcId(svcId);
    }

    /**
     * 根据应用id查询集群信息
     *
     * @param appId 应用id
     * @return 结果
     */
    @Override
    public List<ClusterInfoPO> getClusterInfosByAppId(String appId) {
        return clusterInfoMapper.selectClusterInfoByAppId(appId);
    }

    /**
     * 根据服务id（数据库id）查询集群信息
     *
     * @param svcId 服务id
     * @return 结果
     */
    @Override
    public ClusterInfoPO getClusterInfoBySvcId(String svcId) {
        return clusterInfoMapper.selectClusterInfoBySvcId(svcId);
    }

    @Override
    public void updateUserChooseCloud(UpdateUserChooseParam param) {
        clusterInfoMapper.updateUserChooseCloudById(param.getSvcId(), param.getUserChooseCloud());
    }

    /**
     * 查询所有的集群信息
     *
     * @return 结果
     */
    @Override
    public List<ClusterInfoPO> getClusterInfoByUserChooseCloud(String userChooseCloud) {
        return clusterInfoMapper.selectByUserChooseCloud(userChooseCloud);
    }
}
